package cn.clq.ymcc.mq;


import static cn.clq.ymcc.constants.MqConstants.*;
import cn.clq.ymcc.domain.Login;
import cn.clq.ymcc.dto.CourseOnSendMessageDto;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author clqlongbao
 * @version 1.0
 * @description: TODO
 * @date 2022/7/24 9:28
 */
@Component
public class CourseOnSendProducer {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    //发送短信
    public void sendMessage(CourseOnSendMessageDto messageDto){
        //为了方便消费者重新将message封装成messageDto对象，如果这里直接传对象的话，消费者反序列化会很麻烦
        //所以这里先将messageDto转换成json字符串再传递
        String jsonString = JSON.toJSONString(messageDto);
        System.out.println("传给生产者的数据："+jsonString);
        rocketMQTemplate.sendOneWay(COURSE_MESSAGE_TOPIC+":"+COURSE_MESSAGE_TAGS,jsonString);
        //rocketMQTemplate.sendOneWay("course-message-topic"+":"+"course-message-tags",jsonString);
    }
    //发送邮件
    public void sendEmail(List<Login> logininfo){
        String jsonString = JSON.toJSONString(logininfo);
        rocketMQTemplate.sendOneWay("email-topic:email-tags", jsonString);
    }
    //发送站内信
    public void sendStation(List<Login> logininfo){
        String jsonString = JSON.toJSONString(logininfo);
        rocketMQTemplate.sendOneWay("station-topic:station-tags", jsonString);
    }
}
